package bsts.view.complaint;
import java.awt.*;
import bsts.model.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import bsts.view.util.FieldPanel;
import bsts.controller.ComplaintController;
/*
 * Author 		: Lee Zhong De Rollei
 * Student ID	: A0079953L
 */

public class ViewEditComplaintFrm extends JFrame implements ActionListener{
	
	/*Local attributes=================================================================================================*/
	//required as it references serializable objects for binary saving
	private static final long serialVersionUID = 1L;
	
	//Data Related Variables
	private Complaint complaint;
	private ViewComplaintsFrm caller;
		
	//UI Related Variables
	private FieldPanel fpComplaintID, fpComplaintDt,fpCustName,fpCreatedBy,fpStatus,fpComplaint;
	private JPanel fieldsPanel, btnPanel;

	/*Constructors=======================================================================================================*/	
	public ViewEditComplaintFrm(ViewComplaintsFrm caller, long complaint_Id, int width, int height) {
		setTitle("View/Edit Complaint");
		setSize(width,height);	
		setResizable(false);
		this.caller = caller;
		this.complaint = new ComplaintController().GetComplaint(complaint_Id);
		initialize();
	}
	
	private void initialize(){		
		
		 
		//initialize field panel
		initializeFieldPanel();
		//initialize button panel
		initializeBtnPanel();
		//set frame properties
		getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		getContentPane().add(fieldsPanel);
		getContentPane().add(btnPanel);
//	
	}
	
	private void initializeFieldPanel(){
		//Create fields by reusing field panel control
		fieldsPanel  = new JPanel(new FlowLayout(FlowLayout.LEFT));
		fpComplaintID = new FieldPanel(FieldPanel.PnlType.Text,"Complaint ID",String.valueOf(complaint.getComplaintId()),20,150,250);
		fpComplaintDt = new FieldPanel(FieldPanel.PnlType.Text,"Complaint Date/Time",complaint.getTimestampStr(),20,150,250);
		fpCustName = new FieldPanel(FieldPanel.PnlType.Text,"Customer Nric",complaint.getCustomerNric(),20,150,250);
		fpCreatedBy = new FieldPanel(FieldPanel.PnlType.Text,"Created By",complaint.getUserId(),20,150,250);
		fpStatus= new FieldPanel(FieldPanel.PnlType.RadioBtns,"Status",complaint.getComplaintStatus().name().toString(),20,150,250,ComplaintStatus.class);
		fpComplaint = new FieldPanel(FieldPanel.PnlType.TextArea,"Complaint",complaint.getComplaint(),150,150,380);
		//add fieldpanel to fieldS panel
		fieldsPanel.add(fpComplaintID);
		fieldsPanel.add(fpComplaintDt);
		fieldsPanel.add(fpCustName);
		fieldsPanel.add(fpCreatedBy);
		fieldsPanel.add(fpStatus);
		fieldsPanel.add(fpComplaint);
		//get fields panel properties
		fieldsPanel.setPreferredSize(new Dimension(600,390));
		fieldsPanel.setBorder(BorderFactory.createTitledBorder("View/Edit Complaint"));
	}
  
	private void initializeBtnPanel(){
		
		 btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
		 btnPanel.setPreferredSize(new Dimension(600, 40));
		 btnPanel.add(createBtnWithListener("Save",this,"save"));
		 btnPanel.add(createBtnWithListener("Cancel",this,"cancel"));
		 
	}
	
	private boolean validateSave(){
    	String complaint_text = fpComplaint.getValue();    	    
		if (complaint_text.length()<11){
    		JOptionPane.showMessageDialog(this, "Complaint should not contain less than 10 characters(including space)");
    		return false;
    	}else{
    		return true;
    	}
    	
    }
	
    //wrapper class to basically wrap lines of code
    private static JButton createBtnWithListener(String text, ActionListener list, String cmd){
	   	JButton btn = new JButton(text);
	   	btn.addActionListener(list);
	   	btn.setActionCommand(cmd);
	   	return btn;
	   }
	   	   
	/*public methods for events=========================================================================================*/
	public void actionPerformed(ActionEvent evt){		
    if(evt.getActionCommand().equals("save")){
    	if (validateSave()){
			boolean status;
			complaint.setComplaint(fpComplaint.getValue());
			complaint.setComplaintStatus(fpStatus.getValue().toUpperCase().equals("PENDING")?ComplaintStatus.Pending:ComplaintStatus.Closed);
			status = new ComplaintController().UpdateComplaint(complaint);
			//Display msg box
			if(status){
				caller.refreshGrid();
				JOptionPane.showMessageDialog(this, "Complaint Saved Successfully");
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(this, "System encounter problem when saving complaint");
			}
    	}//end of if validatesave
    }else if(evt.getActionCommand().equals("cancel")){
    	this.dispose();
    }
	}
	
  


}
